package com.bigdata.service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.bigdata.common.utils.StringUtils;
import com.bigdata.common.core.page.TableDataInfo;
import com.bigdata.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.bigdata.service.domain.bo.SearchHistoryBo;
import com.bigdata.service.domain.vo.SearchHistoryVo;
import com.bigdata.service.domain.SearchHistory;
import com.bigdata.service.mapper.SearchHistoryMapper;
import com.bigdata.service.service.ISearchHistoryService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户查询历史记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-23
 */
@RequiredArgsConstructor
@Service
public class SearchHistoryServiceImpl implements ISearchHistoryService {

    private final SearchHistoryMapper baseMapper;

    /**
     * 查询用户查询历史记录
     */
    @Override
    public SearchHistoryVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户查询历史记录列表
     */
    @Override
    public TableDataInfo<SearchHistoryVo> queryPageList(SearchHistoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SearchHistory> lqw = buildQueryWrapper(bo);
        Page<SearchHistoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户查询历史记录列表
     */
    @Override
    public List<SearchHistoryVo> queryList(SearchHistoryBo bo) {
        LambdaQueryWrapper<SearchHistory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SearchHistory> buildQueryWrapper(SearchHistoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SearchHistory> lqw = Wrappers.lambdaQuery();
        lqw.eq(null != bo.getUserId(), SearchHistory::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getQueryContent()), SearchHistory::getQueryContent, bo.getQueryContent());
        lqw.eq(bo.getSearchTime() != null, SearchHistory::getSearchTime, bo.getSearchTime());
        lqw.orderByDesc(SearchHistory::getSearchTime);
        return lqw;
    }

    /**
     * 新增用户查询历史记录
     */
    @Override
    public Boolean insertByBo(SearchHistoryBo bo) {
        SearchHistory add = BeanUtil.toBean(bo, SearchHistory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户查询历史记录
     */
    @Override
    public Boolean updateByBo(SearchHistoryBo bo) {
        SearchHistory update = BeanUtil.toBean(bo, SearchHistory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SearchHistory entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户查询历史记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
